package cc.mrbird.febs.pedometer.controller;

import cc.mrbird.febs.common.annotation.ControllerEndpoint;
import cc.mrbird.febs.common.utils.FebsUtil;
import cc.mrbird.febs.common.entity.FebsConstant;
import cc.mrbird.febs.common.controller.BaseController;
import cc.mrbird.febs.common.entity.FebsResponse;
import cc.mrbird.febs.common.entity.QueryRequest;
import cc.mrbird.febs.pedometer.entity.Batches;
import cc.mrbird.febs.pedometer.entity.Chicken;
import cc.mrbird.febs.pedometer.service.IBatchesService;
import cc.mrbird.febs.pedometer.service.IChickenService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 鸡批次 Controller
 *
 * @author li
 * @date 2020-01-31 11:40:37
 */
@Slf4j
@Validated
@Controller
public class BatchesController extends BaseController {

    @Autowired
    private IBatchesService batchesService;
    @Autowired
    private IChickenService chickenService;

    @GetMapping("batches")
    @ResponseBody
    @RequiresPermissions("batches:list")
    public FebsResponse getAllBatchess(Batches batches) {
        return new FebsResponse().success().data(batchesService.findBatchess(batches));
    }

    @GetMapping("batches/list")
    @ResponseBody
    @RequiresPermissions("batches:list")
    public FebsResponse batchesList(QueryRequest request, Batches batches) {
        Map<String, Object> dataTable = getDataTable(this.batchesService.findBatchess(request, batches));
        return new FebsResponse().success().data(dataTable);
    }

    @ControllerEndpoint(operation = "新增Batches", exceptionMessage = "新增Batches失败")
    @PostMapping("batches")
    @ResponseBody
    @RequiresPermissions("batches:add")
    public FebsResponse addBatches(@Valid Batches batches) {
        this.batchesService.createBatches(batches);
        List<Chicken> chickens = new ArrayList<>();
        Chicken chicken = new Chicken();
        chicken.setBatchesId(batches.getBatchesId());
        chicken.setType(batches.getType());
        for (int i=0; i<batches.getNumber(); i++){
            chickens.add(chicken);
        }
        chickenService.saveBatch(chickens);
        return new FebsResponse().success();
    }

    @ControllerEndpoint(operation = "删除Batches", exceptionMessage = "删除Batches失败")
    @GetMapping("batches/delete/{batchesId}")
    @ResponseBody
    @RequiresPermissions("batches:delete")
    public FebsResponse deleteBatches(@NotBlank(message = "{required}") @PathVariable String batchesId) {
        this.batchesService.deleteBatches(batchesId);
        chickenService.remove(new QueryWrapper<Chicken>().lambda().in(Chicken::getBatchesId, batchesId));
        return new FebsResponse().success();
    }

    @ControllerEndpoint(operation = "修改Batches", exceptionMessage = "修改Batches失败")
    @PostMapping("batches/update")
    @ResponseBody
    @RequiresPermissions("batches:update")
    public FebsResponse updateBatches(Batches batches) {
        this.batchesService.updateBatches(batches);
        return new FebsResponse().success();
    }

    @ControllerEndpoint(operation = "修改Batches", exceptionMessage = "导出Excel失败")
    @GetMapping("batches/excel")
    @ResponseBody
    @RequiresPermissions("batches:export")
    public void export(QueryRequest queryRequest, Batches batches, HttpServletResponse response) {
        List<Batches> batchess = this.batchesService.findBatchess(queryRequest, batches).getRecords();
        ExcelKit.$Export(Batches.class, response).downXlsx(batchess, false);
    }
}
